/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pop3;

/**
 *
 * @author epulapp
 */
public class IHMClient extends javax.swing.JFrame {

    /**
     * Creates new form IHMClient
     */
    private ClientPOP cli;

    public IHMClient() {
        initComponents();

        this.b_quitterServeur.setEnabled(false);
        this.b_valider.setEnabled(false);

        this.buttonGroup1.add(this.rb_connexion);
        this.buttonGroup1.add(this.rb_recuperation);
        this.buttonGroup1.add(this.rb_statut);

        this.rb_connexion.setEnabled(false);
        this.rb_recuperation.setEnabled(false);
        this.rb_statut.setEnabled(false);

        this.tf_login.setEnabled(false);
        this.tf_mdp.setEnabled(false);
        this.tf_numMsg.setEnabled(false);

        this.l_login.setEnabled(false);
        this.l_mdp.setEnabled(false);

        tf_adresseServeur.setText("134.214.119.195");
        cli = new ClientPOP();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        tf_adresseServeur = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_reponse = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        b_connexionServeur = new javax.swing.JButton();
        rb_connexion = new javax.swing.JRadioButton();
        rb_statut = new javax.swing.JRadioButton();
        rb_recuperation = new javax.swing.JRadioButton();
        b_quitterServeur = new javax.swing.JButton();
        tf_login = new javax.swing.JTextField();
        l_login = new javax.swing.JLabel();
        l_mdp = new javax.swing.JLabel();
        tf_mdp = new javax.swing.JTextField();
        tf_numMsg = new javax.swing.JTextField();
        b_valider = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tf_adresseServeur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_adresseServeurActionPerformed(evt);
            }
        });

        ta_reponse.setColumns(20);
        ta_reponse.setRows(5);
        jScrollPane1.setViewportView(ta_reponse);

        jLabel1.setText("Adresse Serveur : ");

        b_connexionServeur.setText("Connexion au Serveur");
        b_connexionServeur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_connexionServeurActionPerformed(evt);
            }
        });

        rb_connexion.setText("Connexion à la boîte mail");
        rb_connexion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_connexionActionPerformed(evt);
            }
        });

        rb_statut.setText("Etat de la boîte mail");
        rb_statut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_statutActionPerformed(evt);
            }
        });

        rb_recuperation.setText("Récupérer le message ");
        rb_recuperation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_recuperationActionPerformed(evt);
            }
        });

        b_quitterServeur.setText("Quitter le serveur");
        b_quitterServeur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_quitterServeurActionPerformed(evt);
            }
        });

        l_login.setText("Login : ");

        l_mdp.setText("Mot de passe : ");

        tf_numMsg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_numMsgActionPerformed(evt);
            }
        });

        b_valider.setText("Valider");
        b_valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_validerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(l_login)
                                .addGap(72, 72, 72)
                                .addComponent(tf_login))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGap(18, 18, 18)
                                    .addComponent(tf_adresseServeur, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(rb_connexion)
                                .addComponent(rb_statut)))
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(b_connexionServeur)
                                .addGap(95, 95, Short.MAX_VALUE)
                                .addComponent(b_quitterServeur)
                                .addGap(29, 29, 29))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rb_recuperation)
                                        .addGap(27, 27, 27)
                                        .addComponent(tf_numMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(l_mdp)
                                        .addGap(18, 18, 18)
                                        .addComponent(tf_mdp, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(b_valider)
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_adresseServeur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(b_connexionServeur)
                    .addComponent(b_quitterServeur))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rb_connexion, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tf_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(l_login)
                            .addComponent(l_mdp)
                            .addComponent(tf_mdp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rb_statut)
                            .addComponent(rb_recuperation)
                            .addComponent(tf_numMsg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(44, 44, 44))
                    .addComponent(b_valider, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_adresseServeurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_adresseServeurActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_adresseServeurActionPerformed

    private void b_connexionServeurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_connexionServeurActionPerformed
        String adresse = this.tf_adresseServeur.getText();
        Boolean connect = this.cli.connect(adresse);

        this.b_quitterServeur.setEnabled(true);
        this.b_valider.setEnabled(true);

        this.rb_connexion.setEnabled(true);
        this.rb_recuperation.setEnabled(true);
        this.rb_statut.setEnabled(true);

        if (connect == true) {
            this.ta_reponse.append("Connexion au serveur de messagerie réussie\n");
        } else {
            this.ta_reponse.append("Connexion au serveur de messagerie échouée\n");
        }


        //this.ta_reponse.setText(retour);
    }//GEN-LAST:event_b_connexionServeurActionPerformed

    private void rb_connexionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_connexionActionPerformed
        if (this.rb_connexion.isSelected()) {
            this.l_login.setEnabled(true);
            this.l_mdp.setEnabled(true);

            this.tf_login.setEnabled(true);
            this.tf_mdp.setEnabled(true);

            this.tf_numMsg.setEnabled(false);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_rb_connexionActionPerformed

    private void tf_numMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_numMsgActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_numMsgActionPerformed

    private void b_quitterServeurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_quitterServeurActionPerformed
        this.ta_reponse.append("Déconnexion du serveur : \n");
        if (!this.cli.equals(null)) {
            String message = this.cli.deconnexion();
            this.ta_reponse.append(message + "\n");

            this.b_quitterServeur.setEnabled(false);
            this.b_valider.setEnabled(false);

            this.rb_connexion.setEnabled(false);
            this.rb_recuperation.setEnabled(false);
            this.rb_statut.setEnabled(false);

            this.tf_login.setEnabled(false);
            this.tf_mdp.setEnabled(false);
            this.tf_numMsg.setEnabled(false);

            this.l_login.setEnabled(false);
            this.l_mdp.setEnabled(false);

        } else {
            this.ta_reponse.append("Problème de création client, veuillez relancer l'application\n");
        }
    }//GEN-LAST:event_b_quitterServeurActionPerformed

    private void rb_statutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_statutActionPerformed
        if (this.rb_statut.isSelected()) {
            this.l_login.setEnabled(false);
            this.l_mdp.setEnabled(false);

            this.tf_login.setEnabled(false);
            this.tf_mdp.setEnabled(false);

            this.tf_numMsg.setEnabled(false);
        }
    }//GEN-LAST:event_rb_statutActionPerformed

    private void rb_recuperationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_recuperationActionPerformed
        if (this.rb_recuperation.isSelected()) {
            this.l_login.setEnabled(false);
            this.l_mdp.setEnabled(false);

            this.tf_login.setEnabled(false);
            this.tf_mdp.setEnabled(false);

            this.tf_numMsg.setEnabled(true);
        }
    }//GEN-LAST:event_rb_recuperationActionPerformed

    private void b_validerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_validerActionPerformed
        // TODO add your handling code here:
        String message = "";
        if (this.rb_connexion.isSelected()) {
            this.ta_reponse.append("Connexion à votre boîte mail : \n");
            String login = this.tf_login.getText();
            String mdp = this.tf_mdp.getText();
            message = this.cli.connexion(login, mdp);
            this.ta_reponse.append(message + "\n\n");

        } else if (this.rb_recuperation.isSelected()) {
            String num = this.tf_numMsg.getText();
            this.ta_reponse.append("Récupération du message " + num + " : \n");

            if (!num.equals("")) {
                int numMsg = Integer.parseInt(num);
                message = this.cli.recuperation(numMsg);
                this.ta_reponse.append(message + "\n\n");
            } else {
                this.ta_reponse.append("Vous devez renseigner un numéro de message");
            }

        } else if (this.rb_statut.isSelected()) {
            this.ta_reponse.append("Consultation de votre boîte mail : \n");
            message = this.cli.statutCourrier();
            this.ta_reponse.append(message + "\n\n");

        } else {
            this.ta_reponse.append("Sélectionner une option \n");
        }
    }//GEN-LAST:event_b_validerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IHMClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IHMClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IHMClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IHMClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new IHMClient().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_connexionServeur;
    private javax.swing.JButton b_quitterServeur;
    private javax.swing.JButton b_valider;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel l_login;
    private javax.swing.JLabel l_mdp;
    private javax.swing.JRadioButton rb_connexion;
    private javax.swing.JRadioButton rb_recuperation;
    private javax.swing.JRadioButton rb_statut;
    private javax.swing.JTextArea ta_reponse;
    private javax.swing.JTextField tf_adresseServeur;
    private javax.swing.JTextField tf_login;
    private javax.swing.JTextField tf_mdp;
    private javax.swing.JTextField tf_numMsg;
    // End of variables declaration//GEN-END:variables
}
